/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ortustve.daemon;

/**
 *
 * @author jphipps
 */
public class processDaemon extends Thread {
	private static Blocking_queue queue = null;
	private Thread_pool pool;

	public processDaemon() {
		queue = new Blocking_queue();
		pool = new Thread_pool(10,10);
		ortustve.api.DebugLog(ortustve.ortusvars.INFO, "processDaemon: Loading");
	}

	public void run() {
		ortustve.api.DebugLog(ortustve.ortusvars.INFO, "processDaemon: Running");
		try {
			Object s;
			while( (s=queue.dequeue()) != null ) {
			   if ( s instanceof  String)
				   break;
			   pool.execute(new ortustve.daemon.daemonProcess(s));
			}
		} catch (Exception ex) {
			ortustve.api.DebugLog(ortustve.ortusvars.ERROR, "processDaemon: exception: " + ex);
		}
		ortustve.api.DebugLog(ortustve.ortusvars.INFO,"processDaemon: Stopping");
	}

	public void Shutdown() {
		AddQueue("Shutdown");
	}

	public void AddQueue(Object cmd) {
	    ortustve.api.DebugLog(ortustve.ortusvars.INFO,"processDaemon: Queue: added: " + (String)((Object[])cmd)[0]);
	    queue.enqueue(cmd);
	}

}
